package com.yylc.cscore.controller;

import cn.hutool.core.util.StrUtil;
import com.yylc.cscore.bean.CustomerBean;
import com.yylc.cscore.dao.CustomerDao;
import com.yylc.cscore.service.CustomerService;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

/**
 * 客服人员Consumer
 *
 * @author xiangnan
 * date 2018/4/27
 */
@RequestMapping("/customer")
@RestController
public class CustomerController {
    private Logger logger = LogManager.getLogger();

    @Resource
    private CustomerService customerService;

    @Resource
    private CustomerDao customerDao;

    @GetMapping("/list")
    public Object getConsumerList() {
        return customerService.getCustomerList();
    }

    @PostMapping("/list/ids")
    public Object getConsumerList(@RequestParam String serviceIds) {
        if (StrUtil.isBlank(serviceIds)) {
            throw new RuntimeException("参数错误");
        }

        return customerService.getCustomerList(serviceIds);
    }

    @PostMapping("/page/list")
    public Object getConsumerPageList(@RequestParam int current, @RequestParam int size) {
        if (current < 1 || size < 1) {
            throw new RuntimeException("参数错误");
        }

        return customerService.getCustomerList(current, size);
    }

    @PostMapping
    public Object addCustomer(CustomerBean bean) {
        if (bean == null || !bean.valid()) {
            throw new RuntimeException("参数错误");
        }

        return customerService.addCustomer(bean);
    }

    @PutMapping
    public Object updateCustomer(CustomerBean bean, HttpSession session) {
        if (bean == null || !bean.valid()) {
            throw new RuntimeException("参数错误");
        }
        return customerService.updateCustomer(bean, session);
    }

    @PutMapping("/password")
    public Object updateCustomerPassword(@RequestParam String serviceId, @RequestParam String password) {
        if (StrUtil.isBlank(serviceId) || StrUtil.isBlank(password)) {
            throw new RuntimeException("参数错误");
        }

        logger.info("更新密码: serviceId={}, newPassword={}", serviceId, password);
        return customerDao.updatePassword(serviceId, password);
    }

    @PutMapping("/delete")
    public Object deleteCustomer(CustomerBean bean) {
        if (bean == null || !bean.valid()) {
            throw new RuntimeException("参数错误");
        }

        return customerService.deleteCustomer(bean);
    }

}
